package com.neusoft.SPNursingHome.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.SPNursingHome.dao.CareItemDao;
import com.neusoft.SPNursingHome.po.CareItem;
import com.neusoft.SPNursingHome.po.CareLevel;
import com.neusoft.SPNursingHome.po.ResponseBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class CareItemServiceImpl implements CareItemService {
    @Autowired
    private CareItemDao careItemDao;

    @Override
    public ResponseBean<List<CareItem>> listCareItem() {
        System.out.println("调试：护理项目列表");
        // 创建 QueryWrapper 查询条件
        QueryWrapper<CareItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("care_item_label", 1); // 添加条件：care_item_label = 1
        // 执行查询
        List<CareItem> result = careItemDao.selectList(queryWrapper);
        ResponseBean<List<CareItem>> responseBean = null;
        if (result == null) {
            responseBean = new ResponseBean<>(500, "获取护理项目列表错误");
        } else {
            responseBean = new ResponseBean<>(result);
        }
        return responseBean;
    }

//    @Override
//    public ResponseBean<List<CareItem>> listCareItemByConditions(String careItemCode, String careItemName, Boolean careItemStatus) {
//        System.out.println("调试：根据编码、名称、状态联合查询护理项目列表");
//        QueryWrapper<CareItem> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("care_item_label", 1); // 添加条件：care_item_label = 1
//        if (careItemCode != null && !careItemCode.trim().isEmpty()) {
//            queryWrapper.like("care_item_code", careItemCode);
//        }
//        if (careItemName != null && !careItemName.trim().isEmpty()) {
//            queryWrapper.like("care_item_name", careItemName);
//        }
//        if (careItemStatus != null) {
//            queryWrapper.eq("care_item_status", careItemStatus);
//        }
//
//        // 执行查询
//        List<CareItem> result = careItemDao.selectList(queryWrapper);
//        // 构建响应对象
//        ResponseBean<List<CareItem>> responseBean = null;
//        if (result == null || result.isEmpty()) {
//            responseBean = new ResponseBean<>(500, "未找到符合条件的护理项目");
//        } else {
//            responseBean = new ResponseBean<>(result);
//        }
//        return responseBean;
//    }

    @Override
    public ResponseBean<IPage<CareItem>> listCareItemByConditions(String careItemCode, String careItemName, Boolean careItemStatus, long page, long size) {
        // 1. 分页对象
        Page<CareItem> pager = new Page<>(page, size);

        System.out.println("调试：根据编码、名称、状态联合查询护理项目列表");
        QueryWrapper<CareItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("care_item_label", 1); // 添加条件：care_item_label = 1
        if (careItemCode != null && !careItemCode.trim().isEmpty()) {
            queryWrapper.like("care_item_code", careItemCode);
        }
        if (careItemName != null && !careItemName.trim().isEmpty()) {
            queryWrapper.like("care_item_name", careItemName);
        }
        if (careItemStatus != null) {
            queryWrapper.eq("care_item_status", careItemStatus);
        }

        // 执行查询
        List<CareItem> result = careItemDao.selectList(queryWrapper);
        // 构建响应对象
        ResponseBean<List<CareItem>> responseBean = null;
        if (result == null || result.isEmpty()) {
            responseBean = new ResponseBean<>(500, "未找到符合条件的护理项目");
        } else {
            responseBean = new ResponseBean<>(result);
        }

        // 3. 执行分页查询
        return new ResponseBean<>(careItemDao.selectPage(pager, queryWrapper));
    }

    @Override
    public ResponseBean<Integer> updateCareItem(CareItem careItemNew) {
        System.out.println("调试：修改护理项目信息");

        // 查询要修改的护理项目
        CareItem careItem = careItemDao.selectById(careItemNew.getCareItemId());
        if (careItem == null) {
            return new ResponseBean<>(500, "未找到指定的护理项目");
        }

        careItem = careItemNew;
        careItem.setCareItemLabel(1);

        // 执行更新操作
        int updateResult = careItemDao.updateById(careItem);
        // 构建响应对象
        ResponseBean<Integer> responseBean = null;
        if (updateResult > 0) {
            responseBean = new ResponseBean<>(updateResult);
        } else {
            responseBean = new ResponseBean<>(500, "修改护理项目失败");
        }
        return responseBean;
    }

    @Override
    public ResponseBean<Integer> addCareItem(CareItem careItem) {
        System.out.println("调试：添加护理项目");

        // 创建新的护理项目对象
        CareItem newCareItem = new CareItem();
        newCareItem = careItem;
        // 添加项目默认显示label状态为1
        newCareItem.setCareItemLabel(1);

        // 执行插入操作
        int insertResult = careItemDao.insert(newCareItem);
        // 构建响应对象
        ResponseBean<Integer> responseBean = null;
        if (insertResult > 0) {
            responseBean = new ResponseBean<>(insertResult);
        } else {
            responseBean = new ResponseBean<>(500, "添加护理项目失败");
        }
        return responseBean;
    }

    @Override
    public ResponseBean<Integer> deleteCareItem(Integer careItemId) {
        System.out.println("调试：逻辑删除护理项目");
        CareItem careItem = careItemDao.selectById(careItemId);
        if (careItem == null) {
            return new ResponseBean<>(500, "未找到指定的护理项目");
        }
        careItem.setCareItemLabel(0); // 将 care_item_label 设置为 0
        int updateResult = careItemDao.updateById(careItem);
        ResponseBean<Integer> responseBean = null;
        if (updateResult > 0) {
            responseBean = new ResponseBean<>(updateResult);
        } else {
            responseBean = new ResponseBean<>(500, "删除护理项目失败");
        }
        return responseBean;
    }

    // 查询所有启用状态的护理项目
    @Override
    public ResponseBean<List<CareItem>> listActiveCareItem() {
        System.out.println("调试：查询所有启用状态的护理项目");
        QueryWrapper<CareItem> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("care_item_label", 1) // 添加条件：care_item_label = 1
                .eq("care_item_status", true); // 添加条件：care_item_status = true

        List<CareItem> result = careItemDao.selectList(queryWrapper);
        ResponseBean<List<CareItem>> responseBean = null;
        if (result == null || result.isEmpty()) {
            responseBean = new ResponseBean<>(500, "未找到启用状态的护理项目");
        } else {
            responseBean = new ResponseBean<>(result);
        }
        return responseBean;
    }
}